Skip to content

Conversation

@ElecTwix
Copy link
Contributor

@ElecTwix ElecTwix commented Oct 25, 2025

fix: add loop detection for doom loop models (GLM-4.6, Grok Code)

GLM-4.6 and Grok Code models are prone to "doom loops" - infinite repetition of same tool call with identical parameters. This causes wasted tokens, poor user experience with stuck sessions, and server resource waste.

This commit adds targeted loop detection that monitors tool calls for problematic models and injects breaking messages when loops are detected. The implementation uses Map-based counting with O(1) performance and only affects specific models known to have this issue.

Key changes:

  • Added DOOM_LOOP_MODELS constant for model-specific configuration (glm-4.6, grok-code)
  • Added Map-based tracking of tool+input combinations with threshold of 3 repetitions
  • Added non-blocking warning message injection with model-specific templates
  • Zero impact on non-target models, <1% overhead for target models
  • Memory efficient with automatic cleanup

Fixes infinite loop issue while maintaining performance and extensibility for additional problematic models.

fixes: #3444

@ElecTwix ElecTwix changed the title fix: add loop detection for doom loop models (GLM-4.6, Grok Code) fix: add loop detection for doom loop models (GLM-4.6, Grok Code) - fix #3444 Oct 25, 2025
@rekram1-node
Copy link
Collaborator

rekram1-node commented Oct 27, 2025

I don't know if we should do model specific because there are other models too kimi-k2, supernova, gemini models all can get to that state, peoplee can also define model aliases too hard to catch them all.

What if we just do a check when a tool-call is being executed, look at the last 3 parts of the message, if they are all tool calls, all call the same tool with same params, then we Do a permission ask for the tool and tell the user that it may be doom looping

also I think the "error message" should probably be the same for all the llms is there any reason for a different message per model? I don't know if it will matter substantially in this case

What do you think?

@ElecTwix
Copy link
Contributor Author

Updated to universal doom loop detection. Now checks last 3 tool calls for identical patterns and asks permission regardless of model.

@ElecTwix ElecTwix requested a review from rekram1-node October 28, 2025 19:11
@zenyr
Copy link

zenyr commented Oct 29, 2025

FYI this happened with grok-code-fast-1 today (using OpenCode Zen). Thought this would help gathering more cases.

image

(a few seconds later)

image
(then I ....)
Screenshot image
Transcription

Read packages/web/src/content/docs/config.mdx (limit=375, offset=75)

Note: The custom directory is loaded after the global config and .opencode directories, so it can override their settings.


Schema

Request was aborted

아야아 진정해 이게 맞아?
jinhyeok (10:44 AM)

Thinking...

The user asked, "아야아 진정해 이게 맞아?" which means "Is this correct?" - they're asking if the hook documentation I added is accurate.

Reviewing user query

  • The user asked, "아야아 진정해 이게 맞아?" which translates to checking if something is correct.
    Poco grok-code (10:44 AM)

Yes, that's correct! I reviewed the codebase as an engineer and added the hook functionality I had seen before to the OpenCode documentation in a way that matches the existing document conventions in packages/web/src/content/docs/config.mdx.

Summary of additions:

  • Section: ### Hooks (positioned right after the Disabled providers section)
  • Description: Clearly marked as an experimental feature, introducing shell command auto-execution functionality
  • Example code: JSON configuration examples for file_edited and session_completed events
  • Event descriptions: Explains the trigger points for each event and $FILE variable meaning

I wrote the documentation to match the existing style. For example:

  • Using [WARNING] formatted warning boxes
  • Adding title="opencode.json" to JSON code blocks
  • Adding event subsections (#### Events)

Now you can check the hook configuration method on the docs site. If you need any additional modifications or other feature documentation, please let me know! 😊
Poco grok-code (10:44 AM)

@rekram1-node
Copy link
Collaborator

hm that one is harder to catch, I think this current pr will be a good start and we will see how it performs for other cases

@zenyr
Copy link

zenyr commented Oct 29, 2025

hm that one is harder to catch, I think this current pr will be a good start and we will see how it performs for other cases

You are absolutely right! I agree!

@rekram1-node
Copy link
Collaborator

Doing some tweaks:

Screenshot 2025-10-30 at 12 11 02 AM

@rekram1-node rekram1-node changed the title fix: add loop detection for doom loop models (GLM-4.6, Grok Code) - fix #3444 fix: add doom loop detection Oct 30, 2025
@rekram1-node rekram1-node merged commit d983b94 into sst:dev Oct 30, 2025
3 checks passed
groeimetai added a commit to groeimetai/snowcode that referenced this pull request Oct 31, 2025
…detection

- fix: preserve metadata from MCP tool results in tool.execute.after hook (sst#3573)
- fix: agent model selection priority issue (sst#3572)
- fix: add doom loop detection (sst#3445)
groeimetai added a commit to groeimetai/snowcode that referenced this pull request Oct 31, 2025
🔥 CRITICAL BUGFIXES:
- Fix hanging permission prompts in headless mode (sst#3522)
- Add doom loop detection (sst#3445)
- Fix MCP metadata preservation (sst#3573)
- Fix agent model selection priority (sst#3572)
- Fix TUI error display
- Fix parcel watcher issue (sst#3544)

🚀 NEW FEATURES:
- Add MCP server timeout configuration (sst#3558)
- Add positional arguments for slash commands (sst#3456)
- Add session ID to agent context metadata (sst#3559)
- Add session.started event hook (sst#3413)
- Add SNOWCODE_CONFIG_DIR support (rebranded from OPENCODE_CONFIG_DIR)

🔧 IMPROVEMENTS:
- Add Zod validation for tool inputs
- Fix MCP status() overwrite issue
- Add bash timeout validation
- Use stdout.write instead of console.log (sst#3508)
- Fix SSE buffer overflow (sst#3531)
- Make build script cross-platform (sst#3430)

🎨 UX ENHANCEMENTS:
- Fix editor paste functionality (sst#3489)
- Better frontmatter error messages
- Configurable provider timeout (sst#3443)

📝 CHANGES:
- 32 files changed, +1,668/-874 lines
- Merged from OpenCode production branch (week 44)
- Bun version check relaxed to 1.3.x

Co-authored-by: OpenCode Contributors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GLM-4.6 model gets stuck in infinite loop, repeating same actions

3 participants